﻿namespace ThresholdAssistant.Redis.Constants
{
    /// <summary>
    /// LUA脚本常量
    /// </summary>
    public static class LuaScriptConstant
    {
        /// <summary>
        /// SetNx脚本
        /// </summary>
        public static readonly string SetNxScript = @$"
		if(redis.call('setnx',KEYS[1],ARGV[1])==1) 
		then
			redis.call('expire',KEYS[1],ARGV[2])
			return 'true'
		else
			if(redis.call('ttl',KEYS[1])==-1)
			then
				redis.call('expire',KEYS[1],ARGV[2])
			end
			return'false'
		end";

		/// <summary>
		/// HsetExpire脚本脚本
		/// </summary>
		public static readonly string HsetExpireScript = @$"
		if(redis.call('exists',KEYS[1])==1)
		then
			redis.call('hset',KEYS[1],ARGV[1],ARGV[2]);
			if(redis.call('ttl',KEYS[1])==-1)
			then
			  redis.call('expire',KEYS[1],ARGV[3]);
			end
			return 'true'
		else
   			redis.call('hset',KEYS[1],ARGV[1],ARGV[2]);	
			redis.call('expire',KEYS[1],ARGV[3]);
			return 'true'
		end";
	}
}